<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 导航栏 -->

    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab :title="obj.name" v-for="obj in channels" :key="obj.id">
        <ArticleListVue :channel="obj"></ArticleListVue>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChennelEditShow = true"
      >
        <van-icon name="wap-nav" />
      </div>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChennelEditShow"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <ArticleList
        :my-channels="channels"
        :active="active"
        @addChannels="onAdd"
        @updata-active="onUpdateActive"
      ></ArticleList>
    </van-popup>
    <!-- 频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannerls } from "@/api/user";
import ArticleListVue from "./components/article-list.vue";
import ArticleList from "./components/channel-edit.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
export default {
  name: "HomeIndex",
  components: {
    ArticleListVue,
    ArticleList,
  },
  data() {
    return {
      active: 0,
      channels: [],
      isChennelEditShow: false,
    };
  },
  created() {
    this.loadChannels();
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    async loadChannels() {
      try {
        // const { data } = await getUserChannerls();
        // this.channels = data.data.channels;
        let channels = [];
        if (this.user) {
          const { data } = await getUserChannerls();
          channels = data.data.channels;
        } else {
          const localChannels = getItem("TOUTIAO_CHANNELS");
          if (localChannels) {
            channels = localChannels;
          } else {
            const { data } = await getUserChannerls();
            channels = data.data.channels;
          }
        }

        this.channels = channels;
      } catch (err) {
        this.$toast("获取频道数据失败");
      }
    },
    onAdd(channel) {
      this.channels.push(channel);
    },
    onUpdateActive(index, isChennelEditShow = true) {
      this.active = index;
      this.isChennelEditShow = isChennelEditShow;
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-top: 168px;
  padding-bottom: 100px;
  .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
    }
    height: 82px;
  }
  /deep/ .van-tab {
    min-width: 200px;
    border-right: 1px solid #edeff3;
    font-size: 30px;
    color: #777;
  }
  .van-tab--active {
    color: #333;
  }
  .van-tabs__nav {
    padding-bottom: 0;
  }
  .van-tabs__line {
    bottom: 8px;
    width: 31px !important;
    height: 6px;
    background-color: #3296fa;
  }
  .placeholder {
    width: 66px;
    height: 82px;
    // 不参与 flex 布局 影响 默认为 1 参与；  0 不参与
    flex-shrink: 0;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    width: 66px;
    height: 82px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    opacity: 0.902;
    .van-icon {
      font-size: 33px;
    }
    &::before {
      content: "";
      position: absolute;
      left: 0;
      width: 2px;
      height: 100%;
      background-image: url(~@/assets/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
</style>
